<template>
  <div>
    <el-card style="width: 600px">
      <el-upload
          style="text-align: center"
          class="avatar-uploader"
          action="http://localhost:9090/file/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
      >
        <img v-if="form.avatarUrl" :src="form.avatarUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>


      <el-form label-width="120px">
        <el-form-item label="用户名称">
          <el-input v-model="form.username" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickname" autocomplete="off" disabled ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.phone" autocomplete="off" maxlength="11" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" autocomplete="off" type="textarea" maxlength="80" show-word-limit></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveUser">保 存</el-button>
        </el-form-item>
      </el-form>
    </el-card>

  </div>
</template>

<script>
export default {
  name: "Person",
  data() {
    return {
      form: {
        username: '',
        nickname: '',
        phone: '',
        email: '',
        address: ''
      },
      user: localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')) : {}//获取浏览器用户信息
    }
  },
  created() {
    this.getUserInfo().then(res => {
      console.log(res)
      this.form = res
    })
  },
  methods: {
    //异步获取用户信息
    async getUserInfo() {
      return (await this.request.get('/user/getUserInfo/' + this.user.username)).data//获取用户信息
    },
    //保存用户信息
    saveUser() {
      this.request.post('/user/saveOrUpdate', this.form).then(res => {
        if (res.code === '200') {
          this.$message.success('保存成功');

          //触及父级组件的方法，更新user中的信息
          this.$emit('refreshUser')
          //更新浏览器用户信息
          this.getUserInfo().then(res => {
            res.token = JSON.parse(localStorage.getItem('user')).token
            localStorage.setItem('user', JSON.stringify(res))
          })

        } else {
          this.$message.error(res.msg);
        }
      });
    },
    handleAvatarSuccess(res) {
      this.form.avatarUrl = res
    },
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>